import { useEnvStore } from '../store/env'
export const OS_SYSTEM_LOCATION_PERMISSION = 'os_system_loaction_permission'
export const APPLICATION_LOCATION_PERMISSION = 'application_location_permission'
export const WXMP_LOCATION_PERMISSION = 'wxmp_location_permission'

/**
 * 判断字符串是不是经过base64加密处理过
 * 字符串只可能包含A-Z，a-z，0-9，+，/，=字符
 * 字符串长度是4的倍数
 * =只会出现在字符串最后，可能没有或者一个等号或者两个等号
 * @param {string} str
 * @returns {boolean}
 */
const base64Pattern = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/i
export function isBase64(str: string): boolean {
  if (str === '' || str.trim() === '') return false
  return base64Pattern.test(str)
}

/**
 * 检查位置权限
 * @returns
 */
export async function checkLocationPermission() {
  const envStore = useEnvStore()
  return new Promise((resolve, reject) => {
    try {
      const res = uni.getSystemInfoSync()
      if (!res.locationEnabled) {
        //locationEnabled:地理位置的系统开关
        reject({
          Location: OS_SYSTEM_LOCATION_PERMISSION,
          value: false,
        })
      } else if (
        !res.locationAuthorized &&
        (!envStore.systemInfo.environment ||
          envStore.systemInfo.environment != 'wxwork' ||
          envStore.systemInfo.platform != 'ios')
      ) {
        // locationAuthorized:允许微信使用定位的开关
        reject({
          Location: APPLICATION_LOCATION_PERMISSION,
          value: false,
        })
      }
    } catch (e) {
      // Do something when catch error
      console.error('getSystemInfoSync is error', e)
    }
    checkMPLocationPermission().then(
      (res) => {
        if (res == 'success') {
          resolve({
            Location: WXMP_LOCATION_PERMISSION,
            value: true,
          })
        }
      },
      (error) => {
        if (error == 'fail') {
          reject({
            Location: WXMP_LOCATION_PERMISSION,
            value: false,
          })
        }
      }
    )
  })
}

export function checkBlutoothPermission() {
  return new Promise((resolve, reject) => {
    uni.getSetting({
      success(res: any) {
        console.error('wxGetSetting', res)
        if (!res.authSetting['scope.bluetooth']) {
          uni.authorize({
            scope: 'scope.bluetooth',
            success(s) {
              resolve('success')
            },
            fail(error) {
              console.error('fail...', error)
              if (error.errMsg == 'authorize:fail:auth deny' || error.errMsg == 'authorize:fail auth deny') {
                resolve('auth_deny')
              } else {
                resolve('fail')
              }
            },
          })
        } else {
          resolve('success')
        }
      },
      fail(error) {
        resolve('fail')
      },
    })
  })
}

/**
 * 检查小程序是否允许使用微信的定位开关
 */
function checkMPLocationPermission() {
  return new Promise((resolve, reject) => {
    uni.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              console.log('authorize', 'success')
              resolve('success')
            },
            fail: () => {
              console.error('authorize', 'fail')
              reject('fail')
            },
          })
        } else if (res.authSetting['scope.userLocation']) {
          console.log('getSetting', 'success')
          resolve('success')
        }
      },
    })
  })
}
